﻿$(function () {
    //注销登录
    $("#logOff").click(function () {
        $.ajax({
            type: 'post',
            dataType: 'Json',
            url: '/Login/LoginOff',
            async: false,
            success: function (data) {
                if (data == "true") {
                    window.location.href = "/Login/Index";
                }
            }
        });
    });

    //获取用户登录状态(0:未登录 1:已登录)
    $.ajax({
        type: "post",
        url: "/Login/GetIsLogin",
        success: function (result) {
            if (result == "0") {
                window.location.href = "/Login/Index";
            }
        }
    });

    GetAuthorizeAction();
    SetPageMenuAndFunction();
});


var roleId = 0;//角色ID
var actionEncodings = "";//功能权限字符串
var roleEncoding = "";//角色编码
var loginUserName = "";//用户名称

//获取角色权限功能配置
function GetAuthorizeAction() {
    //获取当前用户的角色ID
    $.ajax({
        type: "get",
        async: false,
        url: "/Login/GetCookieRoleId",
        success: function (result) {
            if (result == null || result == "") {
                window.location.href = "/Login/Index";
            } else {
                roleId = result;
            }
        }
    });

    //获取当前角色所拥有的功能权限集合
    $.ajax({
        type: "get",
        async: false,
        url: "/Role/GetActionByRoleId",
        data: { roleId: roleId },
        success: function (result) {
            actionEncodings = result;
        }
    });

    //获取当前角色编码
    $.ajax({
        type: "get",
        async: false,
        url: "/Login/GetRoleEncoding",
        data: { roleId: roleId },
        success: function (result) {
            roleEncoding = result;
        }
    });

    //获取用户名称
    $.ajax({
        type: "get",
        async: false,
        url: "/Login/GetCookieUserName",
        success: function (result) {
            loginUserName = result;
        }
    });

}

//根据角色权限显示对象菜单
function SetPageMenuAndFunction() {
    //获取到所有的页面权限和功能权限
    $.ajax({
        type: 'get',
        async: false,
        dataType: 'json',
        url: '/Role/GetPageAndActionConfit',
        success: function (result) {
            if (result != null) {
                $("#side-menu").empty();
                var ulHtml = "";
                var pages = result.pageInfoList;
                for (var i = 0; i < pages.length; i++) {
                    var page = pages[i];
                    if (roleEncoding == "SuperAdmin") {//超级管理员拥有所有菜单权限
                        ulHtml += '<li><a class="J_menuItem" href="' + page.pageUrl + '"><i class="' + page.class + '"></i> <span class="nav-label">' + page.pageName + '</span></a></li>';
                    } else if (actionEncodings.indexOf(page.pageEncoding) != -1) {
                        ulHtml += '<li><a class="J_menuItem" href="' + page.pageUrl + '"><i class="' + page.class + '"></i> <span class="nav-label">' + page.pageName + '</span></a></li>';
                    }
                }

                $("#side-menu").append('<li class="nav-header"><div class="dropdown profile-element"><span><img alt="image" class="img-circle" src="/Content/img/logo.png" /></span><a data-toggle="dropdown" class="dropdown-toggle" href="#"><span class="clear"><span class="block m-t-xs"><strong class="font-bold">您好,欢迎访问</strong></span><span class="text-muted text-xs block" id="userName">' + loginUserName + '<b class="caret"></b></span></span></a></div></li>');
                $("#side-menu").append(ulHtml);
            }
        }
    });

}